﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Imaginecup.util.input;
using Microsoft.Xna.Framework.Input.Touch;
using Imaginecup.util.collision;
using Imaginecup.factory;
using Imaginecup.util;
using imaginecup.util;
using Imaginecup.sprite;
using Imaginecup.util.resourceMan;
using Microsoft.Xna.Framework.Input;

namespace Imaginecup.gameState.menu
{
    public class MenuState : GameState , IMenuController
    {
        private ContentManager m_MenuContent;


        private const int MAX_MENU = 3;

        private Vector2 m_MenuBlockSize;
        private Vector2[] m_MenuPos;

        private Texture2D[] m_TextureArr;
        private Texture2D m_TextureBack;

        private static MenuState m_MenuState = null;


        private IResetController m_ResetController;
        private IChooseControl m_ChoiceController;

        private MenuState()
        {
            m_MenuContent = new ContentManager(m_Game.Services);
            m_MenuContent.RootDirectory = "Content";
        }


        

        public static MenuState GetMenuState()
        {
            if (m_MenuState == null)
            {
                m_MenuState = new MenuState();
            }
            return m_MenuState;
        }


        /************************************************************************/
        /* 초기화 되는 값(메뉴인덱스)                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            Vector2 offset = new Vector2(233, 144);

            m_MenuBlockSize = new Vector2(304, 59);

            m_MenuPos = new Vector2[3];

            m_MenuPos[0] = offset;
            m_MenuPos[1] = new Vector2(offset.X, 206);
            m_MenuPos[2] = new Vector2(offset.X, 297);
        }


        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)                                  */
        /************************************************************************/
        protected override void LoadStateResource()
        {
            m_TextureArr = new Texture2D[3];


            m_TextureBack = m_MenuContent.Load<Texture2D>("menu\\stop_menu");

            m_TextureArr[0] = m_MenuContent.Load<Texture2D>("menu\\stop_menu_continue");
            m_TextureArr[1] = m_MenuContent.Load<Texture2D>("menu\\stop_menu_restart");
            m_TextureArr[2] = m_MenuContent.Load<Texture2D>("menu\\stop_menu_choose");

        }


        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {
            m_MenuContent.Unload();
        }



        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {

            IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();

            if (mouseInput.IsOneClickUp())
                for (int i = 0; i < MAX_MENU; i++)
                {
                    if (Collision.ContainPoint(m_MenuPos[i], m_MenuBlockSize, mouseInput.Position))
                    {
                        StateChange(i);
                        return;
                    }
                }
            base.Update(gameTime);
        }

        private void StateChange(int menuIndex)
        {
            switch (menuIndex)
            {
                case 0:
                    m_GameStateMan.PopState();
                    return;
                case 1:
                    m_GameStateMan.PopState();
                    m_ResetController.Reset();
                    return;
                case 2:
                    m_GameStateMan.PopState();
                    m_ChoiceController.SetChooseStage();
                    return;
            }
        }



        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = m_GraphicData.GetSpriteBatch();
            sb.Begin();
            sb.Draw(m_TextureBack, Vector2.Zero, Color.White);

            for( int i = 0 ; i < 3; i++ )
            {
                DrawMenuBtn(sb, m_TextureArr[i], m_MenuPos[i]);
            }


            //sb.Draw(m_MenuTexture, m_Pos,
             //   new Rectangle(0, 0, (int)m_Size.X, (int)m_Size.Y), Color.White);
            sb.End();
            base.Draw(gameTime);
        }

        private void DrawMenuBtn(SpriteBatch sb , Texture2D texture , Vector2 pos )
        {
            IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();

            if( mouseInput.MouseState.LeftButton == ButtonState.Pressed &&
                Collision.ContainPoint(pos , m_MenuBlockSize , mouseInput.Position) )
            {
                sb.Draw(texture, pos , new Rectangle((int)m_MenuBlockSize.X, 0, 
                    (int)m_MenuBlockSize.X, (int)m_MenuBlockSize.Y) , Color.White);
            }
            else
            {
                sb.Draw(texture , pos , new Rectangle(0 , 0 , 
                    (int)m_MenuBlockSize.X , (int)m_MenuBlockSize.Y) , Color.White);
            }
        }

        public void SetResetController(IResetController resetController)
        {
            m_ResetController = resetController;
        }

        public void SetChooseControl(IChooseControl chooseControl)
        {
            m_ChoiceController = chooseControl;
        }
    }
}
